package com.lanou.myapplication.Toplinefragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapplication.R;
import com.lanou.myapplication.ToplineAdapter.ToplinerecyclerAdapter;
import com.lanou.myapplication.ToplineAdapter.ViewPagerAdapter1;
import com.lanou.myapplication.ToplineDetailActivity;
import com.lanou.myapplication.entity.Topline;
import com.lanou.myapplication.entity.ToplineDetail;
import com.lanou.myapplication.listenerinterface.OnItemClickListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class Toplinefragment extends Fragment implements OnItemClickListener{
    private ViewPager pager;//广告栏部分
    private ViewPagerAdapter1 pagerAdapter;//广告栏适配器对象
    private RequestQueue queue;//定义一个请求队列
    private ImageView current_image;
    private RecyclerView recyclerView;
    //定义内容列表的适配器对象
    private ToplinerecyclerAdapter toplinerecyclerAdapter;

    private Handler handler; //定义一个handler对象,实现广告轮播过程
    private int currentItem = 0;
    private int maxItem = 0;



    private Runnable r = new Runnable() {
        @Override
        public void run() {
            currentItem++;
            maxItem = pagerAdapter.getCount();
            pager.setCurrentItem(currentItem % maxItem);

            handler.postDelayed(r, 4000);
        }
    };



    @Nullable

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_news_toplinepage, null);
        initView(v);
        initData();
        return v;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    private void initView(View view) {
        pager = (ViewPager)view.findViewById(R.id.toplineviewPager);
        //构建适配器对象
        pagerAdapter = new ViewPagerAdapter1(getActivity().getSupportFragmentManager());
        pager.setAdapter(pagerAdapter);//绑定适配器



       recyclerView=(RecyclerView)view.findViewById(R.id.toplinerecyclerView);
        GridLayoutManager gridLayoutManager =new GridLayoutManager(getActivity(),1);
        recyclerView.setLayoutManager(gridLayoutManager);

        //初始化适配器对象
        toplinerecyclerAdapter =new ToplinerecyclerAdapter(getActivity());
        toplinerecyclerAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(toplinerecyclerAdapter);
        //初始化handler对象
        handler = new Handler();
    }

    private void initData() {
        //初始化请求队列
        queue = Volley.newRequestQueue(getContext());

        //获取最新接口消息
        String url = "http://c.m.163.com/nc/article/headline/T1348647909107/0-20.html";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                //按对象解析返回结果
                Topline tl= gson.fromJson(response, Topline.class);
                if (tl != null) {
                    List<Topline.T1348647909107Entity> data = tl.getT1348647909107();
                    if (data != null && data.size() > 0) {
                        pagerAdapter.addData(data);

                        maxItem=data.size();
                        handler.postDelayed(r, 4000);
                        toplinerecyclerAdapter.addData(data);
                    }
                }
            }
        },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        queue.add(request);//将请求加入到请求队列中

    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (queue != null)
            queue.cancelAll(this);
    }

    @Override
    public void OnClick(Topline.T1348647909107Entity entity) {
        Intent intent =new Intent(getContext(),ToplineDetailActivity.class);
        intent.putExtra("docid", entity.getDocid());
        startActivity(intent);
    }
}